﻿@using PerpetuumSoft.Knockout
@using ContractorTimesheet.DataModel
@using ContractorTimesheet.Infrastructure

@model ContractorTimesheet.Models.UserEditorModel

@{
    var ko = Html.CreateKnockoutContext();

    User loginUser = CurrentSession.User;
}

<h2>Contractors</h2>

<form id="myform">
    <div>
        <table border="0" width="100%">
            <tbody>
                <tr>
                    <td colspan="7">
                        <hr />
                    </td>
                </tr>
                @if (loginUser != null && loginUser.IsAdmin)
                {
                    <tr>
                        <td height="30" width="10%">
                            <input type="text" name="newFirstname" placeholder="Firstname" @ko.Bind.Value(m => m.NewFirstname) />
                        </td>
                        <td width="10%">
                            <input type="text" name="newLastname" placeholder="Lastname" @ko.Bind.Value(m => m.NewLastname) />
                        </td>
                        <td width="10%">
                            <input type="text" name="newEmail" size="150" placeholder="Email" @ko.Bind.Value(m => m.NewEmail) />
                        </td>
                        <td width="10%">
                            <input type="text" name="newPO" placeholder="PO" @ko.Bind.Value(m => m.NewPO) />
                        </td>
                        <td width="10%">
                            <input type="text" name="newBadge" placeholder="BadgeNo" @ko.Bind.Value(m => m.NewBadgeNo) />
                        </td>
                        <td align="center" width="10%">
                            @ko.Html.CheckBox(m => m.NewIsActive)
                        </td>
                        <td align="center">
                            <button type="submit">Add</button>
                        </td>
                    </tr>
                }
                else
                {
                    <tr>
                        <td height="30" width="15%">
                            Firstname
                        </td>
                        <td width="15%">
                            Lastname
                        </td>
                        <td width="30%">
                            Email
                        </td>
                        <td width="10%">
                            PO
                        </td>
                        <td width="10%">
                            BadgeNo
                        </td>
                        <td align="center" width="10%">
                            Active
                        </td>
                        <td align="center">
                            &nbsp;
                        </td>
                    </tr>
                }
                <tr>
                    <td colspan="7">
                        <hr />
                    </td>
                </tr>
                @using (var user = ko.Foreach(m => m.UsersInfo))
                {
                    <tr>
                        <td height="30" width="10%">
                            @user.Html.Span(m => m.Firstname)
                        </td>
                        <td>
                            @user.Html.Span(m => m.Lastname)
                        </td>
                        <td>
                            @user.Html.Span(m => m.Email)
                        </td>
                        <td>
                            @user.Html.Span(m => m.PO)
                        </td>
                        <td>
                            @user.Html.Span(m => m.BadgeNo)
                        </td>
                        <td align="center">
                            @user.Html.CheckBox(m => m.IsActive).Disable(m => true)
                        </td>
                        <td align="center">
                            @if (loginUser != null && loginUser.IsAdmin)
                            {
                                @user.Html.HyperlinkButton("Delete", "DeleteUser", "Users", new { index = @user.GetIndex() })
                            }
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
</form>

<script type="text/javascript">
    $(document).ready(function () {
        $("#myform").ajaxForm();
        $("#myform").validate({
            rules: {
                newFirstname: { required: true, maxlength: 50 },
                newLastname: { required: true, maxlength: 50 },
                newEmail: { required: true, maxlength: 50, email: true },
                newPO: { required: true, maxlength: 15 },
                newBadge: { required: true, number: true, maxlength: 10 }
            },
            messages: {
                newFirstname: { required: '', maxlength: '' },
                newLastname: { required: '', maxlength: '' },
                newEmail: { required: '', maxlength: '', email: '' },
                newPO: { required: '', maxlength: '' },
                newBadge: { required: '', number: '', maxlength: '' }
            },
            submitHandler: function () { @ko.ServerAction("AddUser", "Users"); }
        });
    });
</script>

@ko.Apply(Model)
